#!/sbin/openrc-run

name="Apache Traffic Server"
command="/usr/bin/traffic_manager"
command_background="yes"

pidfile="/run/traffic_manager.pid"
ts_pidfile=/var/lib/trafficserver/server.lock
tm_pidfile=/var/lib/trafficserver/manager.lock

tm_name="/usr/bin/traffic_manager"
ts_name="/usr/bin/traffic_server"

extra_started_commands="reload"

depend() {
	need net
	after firewall
}

stop() {
	ebegin "Stopping Apache Traffic Server manager"
	start-stop-daemon --stop --quiet --retry=QUIT/30/KILL/5 --pidfile "$pidfile" --name "$tm_name"
	eend $?

	ebegin "Stopping remaining Apache Traffic Server manager instances"
	start-stop-daemon --stop --quiet --retry=0/30/KILL/5 --exec "$ts_name"
	eend $?

	# Stop traffic_server instance
	ebegin "Stopping remaining Apache Traffic Server instances"
	start-stop-daemon --stop --quiet --retry=QUIT/30/KILL/5 --pidfile "$ts_pidfile" --name "$ts_name"
	eend $?

	# Clean up pidfiles
	rm -f "$ts_pidfile" "$tm_pidfile"
}

reload() {
	ebegin "Reloading Apache Traffic Server configuration"
	traffic_ctl config reload
	eend $?
}
